package com.tpp.threat_perception_platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tpp.threat_perception_platform.dao.HostAppVulMapper;
import com.tpp.threat_perception_platform.param.MyParam;
import com.tpp.threat_perception_platform.pojo.HostAppVul;
import com.tpp.threat_perception_platform.pojo.WeakPwd;
import com.tpp.threat_perception_platform.response.ResponseResult;
import com.tpp.threat_perception_platform.service.HostAppVulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class HostAppVulServiceImpl implements HostAppVulService {
    @Autowired
    private HostAppVulMapper hostAppVulMapper;
    @Override
    public int saveHostAppVul(HostAppVul hostAppVul) {
        // 设置公共属性
        hostAppVul.setDetectTime(new Timestamp(System.currentTimeMillis()));
        // 插入新记录
        return hostAppVulMapper.insertSelective(hostAppVul);
    }

    @Override
    public void deleteByMacAddress(String macAddress) {
        hostAppVulMapper.deleteByMacAddress(macAddress);
    }

    @Override
    public ResponseResult hostAppVulList(MyParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());

        List<HostAppVul> dataList;

        if (param.getMacAddress() != null && !param.getMacAddress().isEmpty()) {
            dataList = hostAppVulMapper.findByMacAddress(param);
        } else if (param.getHostName() != null && !param.getHostName().isEmpty()) {
            dataList = hostAppVulMapper.findByHostName(param);
        } else if (param.getAppVulName() != null && !param.getAppVulName().isEmpty()) {
            dataList = hostAppVulMapper.findByAppVulName(param);
        } else if (param.getAppVulId() != null && !param.getAppVulId().isEmpty()) {
            dataList = hostAppVulMapper.findByAppVulId(param);
        } else {
            dataList = hostAppVulMapper.findAll(param);
        }

        PageInfo<HostAppVul> pageInfo = new PageInfo<>(dataList);
        return new ResponseResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

}
